<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
        <el-form-item label="费用名称">
           <el-input v-model="queryParams.chargeName" size="mini" clearable></el-input>
        </el-form-item>
        <el-form-item label="费用金额">
           <el-input v-model="queryParams.chargeAmount" size="mini" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
            <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
          >删除</el-button>
        </el-col>
      </el-row>
  
      <el-table ref="uiTable" v-loading="loading" :data="chargeList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="index" label="序号" width="50"/>
        <el-table-column label="费用名称" align="center" prop="chargeName" :show-overflow-tooltip="true" />
        <el-table-column label="费用金额" align="center" prop="chargeAmount" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修改时间" align="center" prop="modifyTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.modifyTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" >
          <template slot-scope="scope">
            <el-button
                    size="mini"
                    circle type="primary" icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    title="修改"
            />
            <el-button
                    size="mini"
                    circle type="danger" icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    title="删除"
            />
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="费用名称" prop="chargeName">
            <el-input type="input"  v-model="form.chargeName" placeholder="请输入费用名称"    />
          </el-form-item>
          <el-form-item label="费用金额" prop="chargeAmount" >
            <el-input type="input"  v-model="form.chargeAmount" placeholder="请输入费用金额" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import {pageList, createOrUpdateCharge, deleteById} from "@/api/pay/charge";
  
  export default {
    name: "termTime",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        indexs:[],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 科目表格数据
        chargeList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态数据字典
        statusOptions: [],
        // 日期范围
        dateRange: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          type: undefined,
          status: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: { 
          chargeName: [
            { required: true, message: "费用名称不能为空", trigger: "blur" }
          ],
          chargeAmount: [
            { required: true, message: "费用金额不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询字典类型列表 */
      getList() {
        this.loading = true;
        pageList(this.queryParams).then(response => {
            this.chargeList = response.data.list;
            this.total = response.data.total;
            this.loading = false;
          }
        );
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          type: undefined,
          status: "0",
          remark: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.queryParams={
            chargeName:null,
            chargeAmount:null,
            pageSize:10,
            pageNum:1
        }
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加科目";
      },
      
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.open = true;
        this.form = JSON.parse(JSON.stringify(row))
      },
     
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
                createOrUpdateCharge(this.form).then(response => {
                    if (response.code === 200) {
                        this.msgSuccess("提交成功");
                        this.open = false;
                        this.getList();
                    } else {
                    this.msgError(response.message);
                    }
                });
            
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = [row.id] || this.ids;
        this.$confirm('是否确认删除序号为"' + this.indexs + '"的数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return deleteById(ids);
          }).then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          }).catch(function() {});
      },
      handleSelectionChange(selection){
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      }
  
    }
  };
  </script>
  